#include "youdoencryp.h"

#include <QCryptographicHash>
#include <qdebug.h>

#include "QAesEncryption.h"
YoudoEncryp::YoudoEncryp()
{

}
void YoudoEncryp::encodeAES(QString load,QString key,QString iv)
{

}

QString YoudoEncryp::EncryptionStr(QString str)
{
    std::wstring wString = str.toStdWString();
    static QByteArray key = "86868686m$M&NOS";
    for (int i = 0; i < wString.size(); i++)
    {
       wString[i] = wString[i] ^ key[i % key.size()];
    }
    return QString::fromStdWString(wString).toUtf8();
}

QString YoudoEncryp::Encrypt(QString str,QString key)
{
    QAESEncryption encryption(QAESEncryption::AES_128, QAESEncryption::ECB, QAESEncryption::ZERO);
    QByteArray hashKey = QCryptographicHash::hash(key.toUtf8(), QCryptographicHash::Md5);
    //qDebug()<<hashKey;
    QByteArray encodedText = encryption.encode(QString(str).toUtf8(), hashKey);
    QString miwen = QString::fromLatin1(encodedText.toBase64());
    return miwen;
}

QString YoudoEncryp::deEncrypt(QString str,QString key)
{
    QAESEncryption encryption(QAESEncryption::AES_128, QAESEncryption::ECB, QAESEncryption::ZERO);
    QByteArray hashKey = QCryptographicHash::hash(key.toUtf8(), QCryptographicHash::Md5);
    QByteArray decodedText = encryption.decode(QByteArray::fromBase64(str.toLatin1()), hashKey);
    QString mingwen = QString::fromUtf8(decodedText);
    return mingwen;
}

YoudoEncryp &YoudoEncryp::instance()
{
    static YoudoEncryp thiz;//这种实例化方法会自动回收内存
    return thiz;
}
